package com.rongxun.main.ui.common;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;

import com.rongxun.common.base.mvvm.BaseMvvmActivity;
import com.rongxun.main.BR;
import com.rongxun.main.R;
import com.rongxun.main.databinding.ActivitySplashBinding;
import com.rongxun.main.factory.SplashViewModelFactory;
import com.rongxun.main.viewmodel.SplashViewModel;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;


/**
 * 创建者     forza
 * 创建时间   2019/11/29 18:09
 * 描述
 */
public class SplashActivity extends BaseMvvmActivity<ActivitySplashBinding, SplashViewModel> {

    @Override
    protected int onBindLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected Class<SplashViewModel> onBindViewModel() {
        return SplashViewModel.class;
    }


    @Override
    protected ViewModelProvider.Factory onBindViewModelFactory() {

        return SplashViewModelFactory.getInstance(getApplication());
    }


    @Override
    protected int onBindVariableId() {
        return BR.viewModel;
    }

    @Override
    protected void initViewObservable() {

        mViewModel.getStartActivityEvent().observe(this, new Observer<Class>() {
            @Override
            public void onChanged(@Nullable Class activity) {
                initAnimate(activity);
            }
        });


    }


    @Override
    public void init() {

    }


    private void initAnimate(Class activity) {
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(mBinding.welcomeView, "scaleX", 1f, 1.13f);

        ObjectAnimator animatorY = ObjectAnimator.ofFloat(mBinding.welcomeView, "scaleY", 1f, 1.13f);

        final AnimatorSet set = new AnimatorSet();
        set.setDuration(2000).play(animatorX).with(animatorY);
        set.start();
        set.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                startMainActivity(activity);
            }
        });

    }

    public void startMainActivity(Class activity) {
        startActivity(new Intent(this, activity));
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @Override
    public void showInitLoadView(boolean show) {

    }

    @Override
    public void showNoDataView(boolean show) {

    }

    @Override
    public void showTransLoadingView(boolean show) {

    }

    @Override
    public void showNetWorkErrView(boolean show) {

    }

    @Override
    public void finishActivity() {

    }
}
